<template>
    <h1>用户购买理财页面</h1>

    <table>
        <tr>
            <td>理财产品名称</td>
            <td>
                {{ Finan.F_FName }}
            </td>
        </tr>

        <tr>
            <td>年收益</td>
            <td>
                {{ Finan.F_YearMoney }}
            </td>
        </tr>

        <tr>
            <td>最低金额</td>
            <td>
                {{ Finan.F_MinMoney }}
            </td>
        </tr>

        <tr>
            <td>风险等级</td>
            <td>
                {{ Number(Finan.F_FinanLevel)==1 ?'低风险': Number(Finan.F_FinanLevel)==2 ?'中风险':'高风险'}}
            </td>
        </tr>

        <tr>
            <td>购买金额</td>
            <td>
                <input type="text" v-model="Money" @change="Get"  >
            </td>
        </tr>

        <tr>
            <td>每年预计收益</td>
            <td>
                {{ MaxMoney.toFixed(2)  }}CNY
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input type="button" value="购买" @click="AddUserFinancing" >
            </td>
        </tr>

    </table>

</template>

<script setup lang="ts">
import { ref,onMounted } from 'vue'
import axios from 'axios'
import { useRouter,useRoute } from 'vue-router'
import {useUserStore} from '../../store/index.ts'

const Money = ref('');
const MaxMoney = ref(0);

const Finan = ref({
    F_FName:'',
    F_YearMoney:'',
    F_MinMoney:'',
    F_FinanLevel:'',
})

//接收用户编号
const userStore = useUserStore();
//接收理财编号
const route = useRoute();

const router = useRouter();

console.log(userStore.Uid);
console.log(route.params.Id);

//生命周期函数
onMounted(()=>{
    SaveData();
})

const SaveData = ()=>{
    axios({
        method:'get',
        url:'http://localhost:56707/api/Finan/SaveData',
        params:{
            Id:route.params.Id
        }
    })
    .then(res=>{
        console.log(res.data);
        Finan.value = res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}

const Get = ()=>{
    MaxMoney.value = Number(Money.value) * Number(Finan.value.F_YearMoney) /100 
}

const AddUserFinancing = ()=>{
    axios({
        method:'post',
        url:'http://localhost:56707/api/Finan/AddUserFinancing',
        data:{
            U_Id:userStore.Uid,
            F_FId:route.params.Id,
            UF_Money:Money.value,
        }
    })
    .then(res=>{
        if(res.data>0){
            alert('购买成功');
            router.push('/FinanShow');
        }else{
            alert('购买失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}


</script>
